package com.zr.bookcorner.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.netutils.HRetrofitNetHelper;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.ServiceUtil;
import com.zr.bookcorner.R;
import com.zr.bookcorner.service.HeartService;
import com.zr.bookcorner.websocket.AppResponseDispatcher;
import com.zr.websocket.WebSocketService;
import com.zr.websocket.WebSocketSetting;

public class ConfigSetActivity extends BaseNoIMActivity {
     Button mBtnStart;
     TextView mTvStatus;
    @Override
    protected int getLayoutId() {
        return R.layout.config_set_activity;
    }

    @Override
    protected void init() {
        mBtnStart.setVisibility(View.GONE);
        boolean isRuning = ServiceUtil.isServiceExisted(this, "com.zr.websocket.WebSocketService");
        mTvStatus.append("webSocket is running " + isRuning + "\n");
        boolean isRuningHeart = ServiceUtil.isServiceExisted(this, "com.zr.task.service.HeartService");
        mTvStatus.append("HeartService is running " + isRuningHeart + "\n");
        if (!isRuning  || !isRuningHeart) {
            mBtnStart.setVisibility(View.VISIBLE);
        }

        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRuning) {
                    String uuId = PhoneUtil.getUUID(ConfigSetActivity.this);

                   WebSocketSetting.setConnectUrl(HRetrofitNetHelper.getWebSocketUrl() + uuId);//必选
                    WebSocketSetting.setResponseProcessDelivery(new AppResponseDispatcher(getApplicationContext()));
                    WebSocketSetting.setReconnectWithNetworkChanged(true);

                    //启动 WebSocket 服务
                    stopService(new Intent(ConfigSetActivity.this, WebSocketService.class));
                    startService(new Intent(ConfigSetActivity.this, WebSocketService.class));
                }

                if (isRuningHeart) {
                    stopService(new Intent(ConfigSetActivity.this, HeartService.class));
                    startService(new Intent(ConfigSetActivity.this, HeartService.class));
                }
            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
